package org.beholder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: ddreval
 * Date: 02.10.12
 * Time: 17:36
 * To change this template use File | Settings | File Templates.
 */
public class ViewerServlet extends AbstractServlet {

	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(ViewerServlet.class);

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Shot> list = getShots();
        req.setAttribute("list", list);
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }

    private List<Shot> getShots() {
        List<Shot> list = new ArrayList<Shot>();
        try {
            PreparedStatement ps = conn.prepareStatement("SELECT id, time FROM shots_tbl ORDER BY time");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Shot shot = new Shot();
                shot.setId(rs.getLong(1));
                shot.setTime(rs.getTimestamp(2));
                list.add(shot);
            }
        } catch (SQLException e) {
        	logger.error(e);
        }
        return list;
    }

}
